﻿using System;
using System.Collections.Generic;
using System.Linq;
using ProjBackEnd.DBInstance;
using ProjBackEnd.Lib;
using System.Web;
using ProjBackEnd.Areas.BizProj.Models;
using System.Reflection;
using System.Data;

namespace ProjBackEnd.DAL
{
    /// <summary>
    /// 项目逻辑
    /// </summary>
    public class projLogic
    {
        /// <summary>
        /// 获取项目列表【分页】
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_iPageSize"></param>
        /// <param name="_iCurrPage"></param>
        /// <param name="_iProjid"></param>
        /// <param name="_iCusid"></param>
        /// <param name="_dtSdt"></param>
        /// <param name="_dtEdt"></param>
        /// <param name="_strsPid"></param>
        /// <param name="_strMgrid"></param>
        /// <returns></returns>
        public static RespProj GetProjListPaging(string _strMemberid, string _strComcd, int _iPageSize, int _iCurrPage, int _iProjid, int _iCusid, DateTime? _dtSdt, DateTime? _dtEdt, string[] _strsPid, string _strMgrid)
        {
            RespProj respReturn = new RespProj();
            bool hasNextPage = false;
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new Exception("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = bpctx.VIEW_BIZ_PROJINFO.Count(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                            && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                            && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                            && (_dtEdt == null ? true : n.sdt < _dtEdt) //仅比较开始时间
                                                                            && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                            && (string.IsNullOrEmpty(_strMgrid) ? true : n.mgrid == _strMgrid)
                                                                            && (n.purmemberid == _strMemberid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<ProjInfoExt> lstProjExt = bpctx.VIEW_BIZ_PROJINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                                && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                                                && (_dtEdt == null ? true : n.sdt < _dtEdt)
                                                                                                && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                                                && (string.IsNullOrEmpty(_strMgrid) ? true : n.mgrid == _strMgrid)
                                                                                                && (n.purmemberid == _strMemberid))
                                                                                .OrderBy(n => n.projid)
                                                                                .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                .Select(n => new ProjInfoExt
                                                                                {
                                                                                    comcd = n.comcd,
                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                    createid = n.createid,
                                                                                    creator = n.CREATOR,
                                                                                    ctno = n.ctno,
                                                                                    cusid = n.cusid,
                                                                                    cusnm = n.cusnm,
                                                                                    deftaxrate = n.deftaxrate,
                                                                                    edt = (n.edt == null ? "" : ((DateTime)n.edt).ToString("yyyy-MM-dd")),
                                                                                    findt = (n.findt == null ? "" : ((DateTime)n.findt).ToString("yyyy-MM-dd")),
                                                                                    mgrid = n.mgrid,
                                                                                    manager = n.MANAGER,
                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                    pid = n.pid,
                                                                                    pidnm = n.pidnm,
                                                                                    pprojid = n.pprojid,
                                                                                    projdesc = n.projdesc,
                                                                                    projid = n.projid,
                                                                                    projnm = n.projnm,
                                                                                    pprojnm = n.pprojnm,
                                                                                    remark = n.remark,
                                                                                    sdt = (n.sdt == null ? "" : n.sdt.ToString("yyyy-MM-dd"))
                                                                                }).ToList();
                respReturn.result = new ResultProj { hasnextpage = hasNextPage, pagecount = iPageCount, projinfoextlist = lstProjExt, reccount = iRecTotal };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新项目信息
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateProjInfo(string _strMemberid, ProjInfo _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb)
                {
                    #region 新增
                    case "I":
                        BIZ_PROJTB entityIns = new BIZ_PROJTB();
                        int iProjid = sysLogic.GetRefNo("PROJ");
                        if (iProjid == 0) throw new Exception("获取流水号失败");
                        entityIns.comcd = _collec.comcd;
                        entityIns.createdt = DateTime.Now;
                        entityIns.createid = _strMemberid;
                        entityIns.ctno = _collec.ctno ?? "";
                        entityIns.cusid = _collec.cusid;
                        entityIns.edt = string.IsNullOrEmpty(_collec.edt) ? entityIns.edt : Convert.ToDateTime(_collec.edt);
                        entityIns.findt = string.IsNullOrEmpty(_collec.findt) ? entityIns.findt : Convert.ToDateTime(_collec.findt);
                        entityIns.mgrid = _collec.mgrid ?? "";
                        entityIns.pid = "0";
                        entityIns.pprojid = _collec.pprojid;
                        entityIns.projdesc = _collec.projdesc ?? "";
                        entityIns.projid = iProjid;
                        entityIns.projnm = _collec.projnm ?? "项目" + iProjid.ToString();
                        entityIns.remark = _collec.remark ?? "";
                        entityIns.sdt = Convert.ToDateTime(_collec.sdt);
                        bpctx.BIZ_PROJTB.InsertOnSubmit(entityIns);
                        //项目赋默认权限
                        List<PUR_MANAGERTB_bizproj> lst = bpctx.PUR_MANAGERTB_bizproj.Where(n => n.comcd == _collec.comcd && n.purtp == GlobalSettings.PURTP_PROJ).ToList();
                        List<PUR_PROJ_bizproj> lstPur = new List<PUR_PROJ_bizproj> { new PUR_PROJ_bizproj { memberid = _strMemberid, projid = iProjid } };
                        foreach (PUR_MANAGERTB_bizproj _entity in lst)
                        {
                            if (lstPur.Count(n => n.memberid == _entity.memberid && n.projid == iProjid) > 0) continue;
                            lstPur.Add(new PUR_PROJ_bizproj { memberid = _entity.memberid, projid = iProjid });
                        }
                        bpctx.PUR_PROJ_bizproj.InsertAllOnSubmit(lstPur);
                        break;
                    #endregion
                    #region 更改
                    case "U":
                        BIZ_PROJTB entityUpd = bpctx.BIZ_PROJTB.SingleOrDefault(n => n.projid == _collec.projid);
                        if (entityUpd == default(BIZ_PROJTB)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的项目");
                        if (entityUpd.pid == "9") throw new Exception("项目已完成，不能修改");
                        entityUpd.ctno = string.IsNullOrEmpty(_collec.ctno) ? entityUpd.ctno : _collec.ctno;
                        entityUpd.cusid = _collec.cusid == 0 ? entityUpd.cusid : _collec.cusid;
                        entityUpd.edt = string.IsNullOrEmpty(_collec.edt) ? entityUpd.edt : Convert.ToDateTime(_collec.edt);
                        entityUpd.findt = string.IsNullOrEmpty(_collec.findt) ? entityUpd.findt : Convert.ToDateTime(_collec.findt);
                        entityUpd.mgrid = _collec.mgrid ?? entityUpd.mgrid;
                        entityUpd.projdesc = _collec.projdesc ?? entityUpd.projdesc;
                        entityUpd.projnm = _collec.projnm ?? entityUpd.projnm;
                        entityUpd.remark = _collec.remark ?? entityUpd.remark;
                        entityUpd.comcd = _collec.comcd ?? entityUpd.comcd;
                        entityUpd.sdt = string.IsNullOrEmpty(_collec.sdt) ? entityUpd.sdt : Convert.ToDateTime(_collec.sdt);
                        break;
                    #endregion
                    #region 删除
                    case "D":
                        BIZ_PROJTB entityDel = bpctx.BIZ_PROJTB.SingleOrDefault(n => n.projid == _collec.projid);
                        if (entityDel == default(BIZ_PROJTB)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的项目");
                        if (bpctx.BIZ_PROJTB.Count(n => n.pprojid == _collec.projid) > 0) throw new Exception("该项目有附属项目，不能删除");
                        if (bpctx.BIZ_PROJDETAIL.Count(n => n.projid == _collec.projid) > 0) throw new Exception("该项目下有明细，不能删除");
                        if (bpctx.BIZ_CONTRACTTB.Count(n => n.projid == _collec.projid) > 0) throw new Exception("该项目下有合同，不能删除");
                        if (entityDel.pid == "9") throw new Exception("项目已完成，不能删除");
                        bpctx.BIZ_PROJTB.DeleteOnSubmit(entityDel);
                        bpctx.PUR_PROJ_bizproj.DeleteAllOnSubmit(bpctx.PUR_PROJ_bizproj.Where(n => n.projid == _collec.projid));
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取项目权限【分页】
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_iPageSize"></param>
        /// <param name="_iCurrPage"></param>
        /// <param name="_iProjid"></param>
        /// <param name="_iCusid"></param>
        /// <param name="_dtSdt"></param>
        /// <param name="_dtEdt"></param>
        /// <param name="_strsPid"></param>
        /// <param name="_strMgrid"></param>
        /// <returns></returns>
        public static RespPurProj GetPurProjListPaging(string _strMemberid, string _strComcd, int _iPageSize, int _iCurrPage, int _iProjid, int _iCusid, DateTime? _dtSdt, DateTime? _dtEdt, string[] _strsPid, string _strMgrid)
        {
            RespPurProj respReturn = new RespPurProj();
            bool hasNextPage = false;
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new Exception("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = bpctx.BIZ_PROJTB.Count(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                            && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                            && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                            && (_dtEdt == null ? true : n.sdt < _dtEdt) //仅比较开始时间
                                                                            && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                            && (string.IsNullOrEmpty(_strMgrid) ? true : n.mgrid == _strMgrid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<ProjInfo> lstProj = bpctx.BIZ_PROJTB.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                                && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                                                && (_dtEdt == null ? true : n.sdt < _dtEdt)
                                                                                                && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                                                && (string.IsNullOrEmpty(_strMgrid) ? true : n.mgrid == _strMgrid))
                                                                                .OrderBy(n => n.projid)
                                                                                .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                .Select(n => new ProjInfo
                                                                                {
                                                                                    comcd = n.comcd,
                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                    createid = n.createid,
                                                                                    ctno = n.ctno,
                                                                                    cusid = n.cusid,
                                                                                    edt = (n.edt == null ? "" : ((DateTime)n.edt).ToString("yyyy-MM-dd")),
                                                                                    findt = (n.findt == null ? "" : ((DateTime)n.findt).ToString("yyyy-MM-dd")),
                                                                                    mgrid = n.mgrid,
                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                    pid = n.pid,
                                                                                    pprojid = n.pprojid,
                                                                                    projdesc = n.projdesc,
                                                                                    projid = n.projid,
                                                                                    projnm = n.projnm,
                                                                                    remark = n.remark,
                                                                                    sdt = (n.sdt == null ? "" : n.sdt.ToString("yyyy-MM-dd"))
                                                                                }).ToList();
                respReturn.result = new ResultPurProj { hasnextpage = hasNextPage, pagecount = iPageCount, projinfolist = lstProj, reccount = iRecTotal };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新项目敏感信息
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateProjInfoSens(string _strMemberid, string _strComcd, ProjInfoCore _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_collec.optlb != "U") throw new Exception("操作标识符异常");
                BIZ_PROJTB entityUpd = bpctx.BIZ_PROJTB.SingleOrDefault(n => n.projid == _collec.projid);
                if (entityUpd == default(BIZ_PROJTB)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的项目");
                //if (entityUpd.pid == "9") { 写敏感操作日志 } 
                if (entityUpd.pid != _collec.pid)
                {
                    if (entityUpd.pid == "9") entityUpd.findt = null; //操作已完成的项目进度时，清除实际完成时间
                    if (_collec.pid == "9" && entityUpd.findt == null) entityUpd.findt = DateTime.Now; //自动写入实际完成时间
                    entityUpd.pid = _collec.pid ?? entityUpd.pid;
                    bpctx.SubmitChanges();
                }
                respReturn.statusMsg = "操作成功";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取项目明细列表
        /// </summary>
        /// <param name="_iProjid"></param>
        /// <param name="_iCtid"></param>
        /// <param name="_iCtSubid"></param>
        /// <param name="_iProjdtlid"></param>
        /// <returns></returns>
        public static RespProjDtl GetProjDtlList(int _iProjid,int _iCtid,int _iCtSubid, int _iProjdtlid)
        {
            RespProjDtl respReturn = new RespProjDtl();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ProjDtlInfoExt> lstProjDtl = bpctx.VIEW_BIZ_PROJDTLINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                                && (_iCtid == 0 ? true : n.ctid == _iCtid)
                                                                                                                                && (_iCtSubid == 0 ? true : n.ctsubid == _iCtSubid)
                                                                                                                                && (_iProjdtlid == 0 ? true : n.projdtlid == _iProjdtlid))
                                                                                                                .AsEnumerable()
                                                                                                                .Select(n => new ProjDtlInfoExt
                                                                                                                {
                                                                                                                    comcd = n.comcd,
                                                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd HH:mm:ss"),
                                                                                                                    createid = n.createid,
                                                                                                                    dtlnm = n.dtlnm,
                                                                                                                    dtltp = n.dtltp,
                                                                                                                    model = n.model,
                                                                                                                    spec = n.spec,
                                                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                    projdtlid = n.projdtlid,
                                                                                                                    projid = n.projid,
                                                                                                                    remark = n.remark,
                                                                                                                    unit = n.unit,
                                                                                                                    amt_recprice = n.amt_recprice,
                                                                                                                    amt_rectotal = n.amt_rectotal,
                                                                                                                    amt_refprice = n.amt_refprice,
                                                                                                                    amt_reftotal = n.amt_reftotal,
                                                                                                                    amt_paytotal = n.amt_paytotal,
                                                                                                                    amt_approved = n.amt_approved,
                                                                                                                    selnum = n.recnum,
                                                                                                                    taxrate = n.taxrate,
                                                                                                                    amt_selprice = n.amt_selprice,
                                                                                                                    amt_seltotal = n.amt_seltotal,
                                                                                                                    dtlnum = n.dtlnum,
                                                                                                                    dtltpnm = n.dtltpnm,
                                                                                                                    ctid = n.ctid,
                                                                                                                    ctnm = n.ctnm,
                                                                                                                    ctsubid = n.ctsubid,
                                                                                                                    ctno = n.ctno,
                                                                                                                    projnm = n.projnm,
                                                                                                                    feepaycount = n.feepaycount,
                                                                                                                    feereccount = n.feereccount
                                                                                                                }).OrderBy(p => p.createdt).ToList();
                 

                respReturn.result = new ResultProjDtl { projdtlinfoextlist = lstProjDtl };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新项目明细
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateProjDtlInfo(string _strMemberid, string _strComcd, ProjDtlInfo _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                BIZ_CONTRACT_SUB entityCtSub = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _collec.ctsubid);
                if(entityCtSub==default(BIZ_CONTRACT_SUB)) throw new Exception("未找到编号为 " + _collec.ctsubid.ToString() + " 的合同子项");
                if(entityCtSub.pid=="9") throw new Exception("该合同子项已完结，无法变更明细 ");
                DateTime dtNow = DateTime.Now;
                switch (_collec.optlb)
                {
                    #region 新增
                    case "I":
                        BIZ_PROJDETAIL entityIns = new BIZ_PROJDETAIL();
                        int iProjdtlid = sysLogic.GetRefNo("PDTL");
                        if (iProjdtlid == 0) throw new Exception("获取流水号失败");
                        entityIns.comcd = _strComcd;
                        entityIns.ctsubid = _collec.ctsubid;
                        entityIns.createdt = dtNow;
                        entityIns.createid = _strMemberid;
                        entityIns.dtlnm = _collec.dtlnm??"";
                        entityIns.dtltp = _collec.dtltp;
                        entityIns.model = _collec.model ?? "";
                        entityIns.projdtlid = iProjdtlid;
                        entityIns.projid = 0;
                        entityIns.remark = _collec.remark ?? "";
                        entityIns.spec = _collec.spec??"";
                        entityIns.unit = _collec.unit;
                        entityIns.amt_refprice = _collec.amt_refprice;
                        entityIns.amt_reftotal = _collec.amt_reftotal;
                        entityIns.dtlnum = _collec.dtlnum;
                        entityIns.amt_selprice = _collec.amt_selprice;
                        entityIns.amt_seltotal = _collec.amt_seltotal;
                        bpctx.BIZ_PROJDETAIL.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 更改
                    case "U":
                        BIZ_PROJDETAIL entityUpd = bpctx.BIZ_PROJDETAIL.SingleOrDefault(n => n.projdtlid == _collec.projdtlid);
                        if(entityUpd==default(BIZ_PROJDETAIL)) throw new Exception("未找到编号为 " + _collec.projdtlid.ToString() + " 的条目");
                        if (entityCtSub.pid == "0")
                        {
                            //190129 所有费用均允许清零
                            entityUpd.amt_refprice = _collec.amt_refprice;
                            entityUpd.amt_reftotal = _collec.amt_reftotal;
                            entityUpd.amt_selprice = _collec.amt_selprice;
                            entityUpd.amt_seltotal = _collec.amt_seltotal;
                        }
                        entityUpd.dtltp = string.IsNullOrEmpty(_collec.dtltp) ? entityUpd.dtltp : _collec.dtltp;
                        entityUpd.dtlnum = _collec.dtlnum == 0 ? entityUpd.dtlnum : _collec.dtlnum;
                        entityUpd.dtlnm = _collec.dtlnm ?? entityUpd.dtlnm;
                        entityUpd.model = _collec.model ?? entityUpd.model;
                        entityUpd.spec = _collec.spec ?? entityUpd.spec;
                        entityUpd.remark = _collec.remark ?? entityUpd.remark;
                        entityUpd.unit = _collec.unit ?? entityUpd.unit;
                        break;
                    #endregion
                    #region 删除
                    case "D":
                        BIZ_PROJDETAIL entityDel = bpctx.BIZ_PROJDETAIL.SingleOrDefault(n => n.projdtlid == _collec.projdtlid);
                        if (entityDel == default(BIZ_PROJDETAIL)) throw new BizException("未找到编号为 " + _collec.projdtlid.ToString() + " 的项目明细");
                        if (bpctx.BIZ_FEE_DETAIL.Count(n => n.projdtlid == _collec.projdtlid) > 0) throw new BizException("编号为" + _collec.projdtlid + "的条目有费用，不能删除");
                        bpctx.BIZ_PROJDETAIL.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            
            catch(BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg =  ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 项目明细移交
        /// </summary>
        /// <param name="_iCtsubid_target"></param>
        /// <param name="_iCtsubid_sour"></param>
        /// <param name="_lstProjdtl"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo ProjDtlTransfer(int _iCtsubid_target,int _iCtsubid_sour,List<int> _lstProjdtl)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                BIZ_CONTRACT_SUB entitySour = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _iCtsubid_sour);
                if (entitySour == default(BIZ_CONTRACT_SUB)) throw new BizException("未找到源合同子项");
                if(entitySour.pid=="9") throw new BizException("源合同子项已完成，无法转移");
                BIZ_CONTRACT_SUB entityTarget = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _iCtsubid_target);
                if (entityTarget == default(BIZ_CONTRACT_SUB)) throw new BizException("未找到目标合同子项");
                if(entityTarget.pid=="9") throw new BizException("目标合同子项已完成，无法转移");
                BIZ_CONTRACTTB entityCtTarget = bpctx.BIZ_CONTRACTTB.SingleOrDefault(n => n.ctid == entityTarget.ctid);
                if (entityCtTarget == default(BIZ_CONTRACTTB)) throw new BizException("未找到目标合同");
                if (entityCtTarget.pid == "9") throw new BizException("目标合同已完成，无法转移");

                List<BIZ_PROJDETAIL> lstProjdtl = bpctx.BIZ_PROJDETAIL.Where(n => n.ctsubid == _iCtsubid_sour && _lstProjdtl.Contains(n.projdtlid)).ToList();
                List<BIZ_FEE_DETAIL> lstFee = bpctx.BIZ_FEE_DETAIL.Where(n => n.ctsubid == _iCtsubid_sour && _lstProjdtl.Contains(n.projdtlid)).ToList();
                foreach(BIZ_PROJDETAIL _entityProjdtl in lstProjdtl) { _entityProjdtl.ctsubid = _iCtsubid_target; }
                foreach (BIZ_FEE_DETAIL _entityFee in lstFee)
                {
                    _entityFee.ctsubid = _iCtsubid_target;
                    _entityFee.projid = entityCtTarget.projid;
                    _entityFee.ctid = entityCtTarget.ctid;
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取项目审批
        /// </summary>
        /// <param name="_iProjid"></param>
        /// <param name="_iApplyid"></param>
        /// <returns></returns>
        public static RespProjApply GetProjApply(int _iProjid,int _iApplyid)
        {
            RespProjApply respReturn = new RespProjApply();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ProjApplyExt> lstApplyExt = bpctx.VIEW_BIZ_PROJAPPLYINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                                    && (_iApplyid == 0 ? true : n.applyid == _iApplyid)).AsEnumerable()
                                                                                                                    .Select(n => new ProjApplyExt
                                                                                                                    {
                                                                                                                        amount = n.amount,
                                                                                                                        applyid = n.applyid,
                                                                                                                        createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                                                        createid = n.createid,
                                                                                                                        creator = n.CREATOR,
                                                                                                                        flowid = n.flowid,
                                                                                                                        optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                        pid = n.pid,
                                                                                                                        projid = n.projid,
                                                                                                                        reviewer = n.REVIEWER,
                                                                                                                        supid = n.supid,
                                                                                                                        supplier = n.SUPPLIER
                                                                                                                    }).ToList();
                respReturn.result = new ResultProjApply { projapplylist = lstApplyExt };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 项目申请单常规信息维护
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <param name="_lstDtl"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateProjApply(string _strMemberid,string _strComcd,ProjApplyInfo _collec,List<ProjDtlCore> _lstDtl)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                BIZ_PROJTB entityProj = bpctx.BIZ_PROJTB.SingleOrDefault(n => n.projid == _collec.projid);
                if (entityProj == default(BIZ_PROJTB)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的项目");
                if (entityProj.pid == "9") throw new Exception("该项目已结束，无法变更信息 ");
                switch (_collec.optlb)
                {
                    #region 新增
                    case "I":
                        if (_lstDtl.Count == 0) throw new Exception("申请单明细不能为空");
                        BIZ_PROJAPPLY entityIns = new BIZ_PROJAPPLY();
                        int iApplyid = sysLogic.GetRefNo("APPLY");
                        if (iApplyid == 0) throw new Exception("获取流水号失败");
                        entityIns.amount = 0;
                        entityIns.applyid = iApplyid;
                        entityIns.createdt = DateTime.Now;
                        entityIns.createid = _strMemberid;
                        entityIns.flowid = _collec.flowid;
                        entityIns.comcd = _strComcd;
                        entityIns.pid = "0";
                        entityIns.projid = _collec.projid;
                        entityIns.remark = _collec.remark ?? "";
                        entityIns.reviewid = "";
                        entityIns.supid = _collec.supid;
                        bpctx.BIZ_PROJAPPLY.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 更改
                    case "U":
                        BIZ_PROJAPPLY entityUpd = bpctx.BIZ_PROJAPPLY.SingleOrDefault(n => n.applyid == _collec.applyid);
                        if (entityUpd == default(BIZ_PROJAPPLY)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的申请单");
                        if (string.Compare(entityUpd.pid, "2") < 0) throw new Exception("申请单（编号 " + entityUpd.applyid.ToString() + "）已申请完成，如需修改，请发起撤销流程");
                        entityUpd.remark = _collec.remark ?? "";
                        entityUpd.supid = _collec.supid;
                        break;
                    #endregion
                    #region 删除
                    case "D":
                        BIZ_PROJAPPLY entityDel = bpctx.BIZ_PROJAPPLY.SingleOrDefault(n => n.applyid == _collec.applyid);
                        if (entityDel == default(BIZ_PROJAPPLY)) throw new Exception("未找到编号为 " + _collec.projid.ToString() + " 的申请单");
                        if (string.Compare(entityDel.pid, "2") < 0) throw new Exception("申请单（编号 " + entityDel.applyid.ToString() + "）已申请完成，如需删除，请发起撤销流程");
                        bpctx.BIZ_PROJAPPLY.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取业务费用明细列表
        /// </summary>
        /// <param name="_strMemberid">请求人id（暂无用处）</param>
        /// <param name="_strComcd">公司代码</param>
        /// <param name="_iProjid">项目id</param>
        /// <param name="_iProjdtlid">合同明细id</param>
        /// <param name="_iBillid">账单id</param>
        /// <param name="_strPid">费用进程</param>
        /// <param name="_strRplb">收付标志</param>
        /// <param name="_iCusid">客户id</param>
        /// <param name="_iCtid">合同id</param>
        /// <param name="_iCtSubid">合同子项id</param>
        /// <param name="_strCreateid">建档人id</param>
        /// <returns></returns>
        public static RespBizFee GetBizFeeList(string _strMemberid, string _strComcd, int _iProjid, int _iProjdtlid, int _iBillid, string _strPid,string _strRplb,int _iCusid,int _iCtid,int _iCtSubid,string _strCreateid)
        {
            RespBizFee respReturn = new RespBizFee();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<BizFeeExtInfo> lst = bpctx.VIEW_BIZ_FEEDETAILINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                       && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                                                       && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                                                       && (_iProjdtlid == 0 ? true : (_iProjdtlid == -1 ? n.projdtlid == 0 : n.projdtlid == _iProjdtlid))
                                                                                                                       && (_iBillid == 0 ? true : n.billid == _iBillid)
                                                                                                                       && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                                       && (_iCtid == 0 ? true : n.ctid == _iCtid)
                                                                                                                       && (_iCtSubid == 0 ? true : n.ctsubid == _iCtSubid)
                                                                                                                       && (n.comcd == _strComcd)
                                                                                                                       && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)).AsEnumerable()
                                                                                                        .Select(n => new BizFeeExtInfo
                                                                                                        {
                                                                                                            amt_payprice = n.amt_payprice,
                                                                                                            amt_paytotal = n.amt_paytotal,
                                                                                                            amt_recprice = n.amt_recprice,
                                                                                                            amt_rectotal = n.amt_rectotal,
                                                                                                            billid = n.billid,
                                                                                                            brand = n.brand,
                                                                                                            brandnm = n.brandnm,
                                                                                                            billpid = n.billpid,
                                                                                                            billpidnm = n.billpidnm,
                                                                                                            comcd = n.comcd,
                                                                                                            createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                                            creator = n.creator,
                                                                                                            ctid = n.ctid,
                                                                                                            ctsubid = n.ctsubid,
                                                                                                            cuscd = n.cuscd,
                                                                                                            cusid = n.cusid,
                                                                                                            cusnm = n.cusnm,
                                                                                                            feeid = n.feeid,
                                                                                                            invotp = n.invotp,
                                                                                                            invotpnm = n.invotpnm,
                                                                                                            model = n.model,
                                                                                                            num = n.num,
                                                                                                            objdesc = n.objdesc,
                                                                                                            objtp = n.objtp,
                                                                                                            objtpnm = n.objtpnm,
                                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                            pid = n.pid,
                                                                                                            piddesc = n.piddesc,
                                                                                                            projdtlid = n.projdtlid,
                                                                                                            projid = n.projid,
                                                                                                            projdtlnm = n.dtlnm,
                                                                                                            projnm = n.projnm,
                                                                                                            purchasdt = n.purchasdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                            remark = n.remark,
                                                                                                            spec = n.spec,
                                                                                                            taxrate = n.taxrate,
                                                                                                            rplb = n.rplb,
                                                                                                            unit = n.unit
                                                                                                        }).ToList();
                respReturn.result = new ResultBizFee { bizfeelist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }
        /// <summary>
        /// 更新业务费用基本信息
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateBizFeeBaseInfo(string _strMemberid,string _strComcd,BizFeeBaseInfo _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                DateTime dtNow = DateTime.Now;
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        if (_collec.projid == 0 || _collec.ctid == 0) throw new BizException("项目或合同流水号错误");
                        int iHeaderCount;
                        if (_collec.projdtlid != 0)
                        {
                            iHeaderCount = bpctx.BIZ_PROJDETAIL.Count(n => n.projdtlid == _collec.projdtlid && n.comcd == _collec.comcd);
                            if (iHeaderCount == 0) throw new BizException("未找到本公司下的归属项目明细。#" + _collec.projdtlid.ToString());
                        }
                        else
                        {
                            iHeaderCount = bpctx.BIZ_CONTRACTTB.Count(n => n.ctid == _collec.ctid && n.comcd == _collec.comcd);
                            if (iHeaderCount == 0) throw new BizException("未找到本公司下的归属合同。#" + _collec.ctid.ToString());
                        }
                        int iFeeid = sysLogic.GetRefNo("FEE");
                        if (iFeeid == 0) throw new BizException("获取流水号失败");
                        BIZ_FEE_DETAIL entityIns = new BIZ_FEE_DETAIL();
                        if (_collec.rplb.ToUpper() == GlobalSettings.FEELB_RECEIVE) //应收费用
                        {
                            entityIns.amt_recprice = _collec.amt_recprice;
                            entityIns.amt_rectotal = _collec.amt_rectotal;
                            entityIns.amt_payprice = entityIns.amt_paytotal = 0;
                        }
                        else if (_collec.rplb.ToUpper() == GlobalSettings.FEELB_PAY) //应付费用
                        {
                            entityIns.amt_payprice = _collec.amt_payprice;
                            entityIns.amt_paytotal = _collec.amt_paytotal;
                            entityIns.amt_recprice = entityIns.amt_rectotal = 0;
                        }
                        else throw new BizException("收付费标志异常");
                        entityIns.autolb = GlobalSettings.NOLB;
                        entityIns.billid = _collec.billid;
                        entityIns.brand = _collec.brand ?? "";
                        entityIns.comcd = _strComcd;
                        entityIns.createdt = dtNow;
                        entityIns.createid = _strMemberid;
                        entityIns.projid = _collec.projid;
                        entityIns.ctid = _collec.ctid;
                        entityIns.ctsubid = _collec.ctsubid;
                        entityIns.cusid = _collec.cusid;
                        entityIns.feeid = iFeeid;
                        entityIns.invotp = _collec.invotp ?? "";
                        entityIns.model = _collec.model ?? "";
                        entityIns.num = _collec.num;
                        entityIns.objdesc = _collec.objdesc ?? "";
                        entityIns.objtp = _collec.objtp ?? "";
                        entityIns.pid = (_collec.billid == 0 ? "0" : "1"); //根据是否挂载账单流水号决定状态是【建档】或【已做账单】
                        entityIns.projdtlid = _collec.projdtlid;
                        entityIns.remark = _collec.remark ?? "";
                        entityIns.rplb = _collec.rplb;
                        entityIns.spec = _collec.spec ?? "";
                        entityIns.taxrate = _collec.taxrate;
                        entityIns.unit = _collec.unit ?? "";
                        entityIns.purchasdt = string.IsNullOrEmpty(_collec.purchasdt) ? dtNow : Convert.ToDateTime(_collec.purchasdt);
                        bpctx.BIZ_FEE_DETAIL.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        BIZ_FEE_DETAIL entityUpd = bpctx.BIZ_FEE_DETAIL.SingleOrDefault(n => n.feeid == _collec.feeid);
                        if (entityUpd == default(BIZ_FEE_DETAIL)) throw new BizException("未找到要操作的费用条目");
                        if (entityUpd.billid > 0) throw new BizException("该费用条目已参与制作账单，请先删除关联账单【" + entityUpd.billid + "】");
                        if (entityUpd.autolb == GlobalSettings.YESLB && entityUpd.rplb == GlobalSettings.FEELB_RECEIVE && _collec.projdtlid > 0)
                        {
                            if (_collec.cusid != 0 && _collec.cusid != entityUpd.cusid) throw new BizException("自动生成的项目应收费用无法变更应收客户");
                            if (_collec.taxrate != 0 && _collec.taxrate != entityUpd.taxrate) throw new BizException("自动生成的项目应收费用无法变更税率");
                        }
                        if (entityUpd.rplb == GlobalSettings.FEELB_RECEIVE)
                        {
                            entityUpd.amt_recprice = entityUpd.amt_recprice == _collec.amt_recprice ? entityUpd.amt_recprice : _collec.amt_recprice;
                            entityUpd.amt_rectotal = entityUpd.amt_rectotal == _collec.amt_rectotal ? entityUpd.amt_rectotal : _collec.amt_rectotal;
                        }
                        else
                        {
                            entityUpd.amt_payprice = entityUpd.amt_payprice == _collec.amt_payprice ? entityUpd.amt_payprice : _collec.amt_payprice;
                            entityUpd.amt_paytotal = entityUpd.amt_paytotal == _collec.amt_paytotal ? entityUpd.amt_paytotal : _collec.amt_paytotal;
                        }
                        entityUpd.cusid = _collec.cusid == 0 ? entityUpd.cusid : _collec.cusid;
                        entityUpd.brand = string.IsNullOrEmpty(entityUpd.brand) ? entityUpd.brand : _collec.brand;
                        entityUpd.invotp = string.IsNullOrEmpty(entityUpd.invotp) ? entityUpd.invotp : _collec.invotp;
                        entityUpd.model = string.IsNullOrEmpty(entityUpd.model) ? entityUpd.model : _collec.model;
                        entityUpd.num = _collec.num == 0 ? entityUpd.num : _collec.num;
                        entityUpd.objdesc = entityUpd.objdesc == _collec.objdesc ? entityUpd.objdesc : _collec.objdesc;
                        entityUpd.objtp = entityUpd.objtp == _collec.objtp ? entityUpd.objtp : _collec.objtp;
                        entityUpd.remark = entityUpd.remark == _collec.remark ? entityUpd.remark : _collec.remark;
                        entityUpd.spec = entityUpd.spec == _collec.spec ? entityUpd.spec : _collec.spec;
                        entityUpd.taxrate = entityUpd.taxrate == _collec.taxrate ? entityUpd.taxrate : _collec.taxrate;
                        entityUpd.unit = entityUpd.unit == _collec.unit ? entityUpd.unit : _collec.unit;
                        entityUpd.purchasdt = string.IsNullOrEmpty(_collec.purchasdt) ? entityUpd.purchasdt : Convert.ToDateTime(_collec.purchasdt);
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        BIZ_FEE_DETAIL entityDel = bpctx.BIZ_FEE_DETAIL.SingleOrDefault(n => n.feeid == _collec.feeid);
                        if (entityDel == default(BIZ_FEE_DETAIL)) throw new BizException("未找到要操作的费用条目");
                        if (entityDel.billid > 0) throw new BizException("该费用条目已参与制作账单，请先删除关联账单【" + entityDel.billid + "】");
                        bpctx.BIZ_FEE_DETAIL.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 根据业务费用id获取费用归属信息
        /// </summary>
        /// <param name="_iFeeid">业务费用id</param>
        /// <returns></returns>
        public static RespBizFeeHeader GetBizFeeHeaderInfo(int _iFeeid)
        {
            RespBizFeeHeader respReturn = new RespBizFeeHeader();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<BizFeeHeader> lst = bpctx.VIEW_BIZ_FEEDETAIL_HEADER.Where(n => n.feeid == _iFeeid).AsEnumerable()
                                                                                                               .Select(n => new BizFeeHeader
                                                                                                               {
                                                                                                                   ctnm = n.ctnm,
                                                                                                                   ctsubnm = n.subnm,
                                                                                                                   feeid = n.feeid,
                                                                                                                   projdtlnm = n.dtlnm,
                                                                                                                   projnm = n.projnm
                                                                                                               }).ToList();
                respReturn.result = new ResultBizFeeHeader { headerlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取业务合同【分页】
        /// ****Memberid未来用于权限判定
        /// </summary>
        /// <param name="_strMember"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_iPageSize"></param>
        /// <param name="_iCurrPage"></param>
        /// <param name="_iProjid"></param>
        /// <param name="_iCtid"></param>
        /// <param name="_iCusid"></param>
        /// <param name="_strPid"></param>
        /// <param name="_dtSdt"></param>
        /// <param name="_dtEdt"></param>
        /// <returns></returns>
        public static RespBizContract GetBizContractPaging(string _strMember,string _strComcd, int _iPageSize, int _iCurrPage, int _iProjid,int _iCtid,int _iCusid,string _strPid, DateTime? _dtSdt,DateTime? _dtEdt)
        {
            RespBizContract respReturn = new RespBizContract();
            bool hasNextPage = false;
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new Exception("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = bpctx.VIEW_BIZ_CONTRACTINFO.Count(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                        && (n.comcd == _strComcd)
                                                                        && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                        && (_dtSdt == null ? true : n.signdt > _dtSdt)
                                                                        && (_dtEdt == null ? true : n.signdt < _dtEdt)
                                                                        && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)
                                                                        && (_iCtid == 0 ? true : n.ctid == _iCtid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<BizContractExtInfo> lstCtExt = bpctx.VIEW_BIZ_CONTRACTINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                                && (n.comcd == _strComcd)
                                                                                                                                && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                                                && (_dtSdt == null ? true : n.signdt > _dtSdt)
                                                                                                                                && (_dtEdt == null ? true : n.signdt < _dtEdt)
                                                                                                                                && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)
                                                                                                                                && (_iCtid == 0 ? true : n.ctid == _iCtid))
                                                                                .OrderBy(n => n.projid)
                                                                                .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                .Select(n => new BizContractExtInfo
                                                                                {
                                                                                    amt_final = n.amt_final,
                                                                                    amt_total = n.amt_total,
                                                                                    comcd = n.comcd,
                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                    creator = n.creator,
                                                                                    ctid = n.ctid,
                                                                                    ctnm = n.ctnm,
                                                                                    ctno = n.ctno,
                                                                                    cusid = n.cusid,
                                                                                    cusnm = n.cusnm,
                                                                                    cusnm_alias = n.cusnm_alias,
                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                    pid = n.pid,
                                                                                    projid = n.projid,
                                                                                    remark = n.remark,
                                                                                    signdt = n.signdt.ToString("yyyy-MM-dd")
                                                                                }).ToList();
                respReturn.result = new ResultBizContract { ctextinfolist = lstCtExt, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新业务合同基本信息
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateBizContractBaseInfo(string _strMemberid,BizContractBaseInfo _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                DateTime dtNow = DateTime.Now;
                switch (_collec.optlb)
                {
                    #region 增加
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iCtid = sysLogic.GetRefNo("CT");
                        if (iCtid == 0) throw new BizException("获取流水号失败");
                        if (_collec.projid == 0) throw new BizException("项目id不能为0");
                        int iSelfProj = bpctx.BIZ_PROJTB.Count(n => n.projid == _collec.projid && n.comcd == _collec.comcd);
                        if (iSelfProj == 0) throw new BizException("未找到本公司下的归属项目。#" + _collec.projid.ToString());
                        BIZ_CONTRACTTB entityIns = new BIZ_CONTRACTTB
                        {
                            acclv = 0,
                            amt_final = _collec.amt_final,
                            amt_total = _collec.amt_total,
                            comcd = _collec.comcd,
                            createdt = dtNow,
                            createid = _strMemberid,
                            ctid = iCtid,
                            ctnm = _collec.ctnm ?? "",
                            ctno = _collec.ctno ?? "",
                            cttp = 0,
                            cusid = _collec.cusid,
                            pid = "0",
                            projid = _collec.projid,
                            remark = _collec.remark ?? "",
                            signdt = string.IsNullOrEmpty(_collec.signdt) ? dtNow : Convert.ToDateTime(_collec.signdt)
                        };
                        bpctx.BIZ_CONTRACTTB.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        BIZ_CONTRACTTB entityUpd = bpctx.BIZ_CONTRACTTB.SingleOrDefault(n => n.ctid == _collec.ctid);
                        if (entityUpd == default(BIZ_CONTRACTTB)) throw new BizException("未找到要操作的记录");
                        if (entityUpd.pid == "9") throw new BizException("合同已完结，无法修改");
                        entityUpd.ctno = _collec.ctno ?? entityUpd.ctno;
                        entityUpd.ctnm = _collec.ctnm ?? entityUpd.ctnm;
                        entityUpd.remark = _collec.remark ?? entityUpd.remark;
                        entityUpd.comcd = _collec.comcd ?? entityUpd.comcd;
                        entityUpd.signdt = string.IsNullOrEmpty(_collec.signdt) ? entityUpd.signdt : Convert.ToDateTime(_collec.signdt);
                        entityUpd.cusid = _collec.cusid == 0 ? entityUpd.cusid : _collec.cusid;
                        entityUpd.amt_final = _collec.amt_final == 0 ? entityUpd.amt_final : _collec.amt_final;
                        entityUpd.amt_total = _collec.amt_total == 0 ? entityUpd.amt_total : _collec.amt_total;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        BIZ_CONTRACTTB entityDel = bpctx.BIZ_CONTRACTTB.SingleOrDefault(n => n.ctid == _collec.ctid);
                        if (entityDel == default(BIZ_CONTRACTTB)) throw new BizException("未找到要操作的记录");
                        if (entityDel.pid == "9") throw new BizException("合同已完结，无法修改");
                        if (bpctx.BIZ_CONTRACT_SUB.Count(n => n.ctid == entityDel.ctid) > 0) throw new BizException("有子记录，不能删除");
                        if (bpctx.BIZ_PROJTB.Count(n => n.projid == entityDel.projid && n.pid == "9") > 0) throw new BizException("归属项目已完结，不能删除");
                        //****需加判操作权限
                        bpctx.BIZ_CONTRACTTB.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新业务合同进程
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateBizContractProc(string _strMemberid,string _strComcd,BizContractCore _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if(_collec.optlb!=GlobalSettings.ENTITYOPERATION_UPDATE) throw new Exception("操作标识符异常");
                //****需加判操作权限
                BIZ_CONTRACTTB entityUpd = bpctx.BIZ_CONTRACTTB.SingleOrDefault(n => n.ctid == _collec.ctid);
                if (entityUpd == default(BIZ_CONTRACTTB)) throw new BizException("未找到要操作的记录");
                entityUpd.pid = string.IsNullOrEmpty(_collec.pid)?entityUpd.pid:_collec.pid;
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取业务子合同
        /// </summary>
        /// <param name="_iProjid"></param>
        /// <param name="_iCtid"></param>
        /// <param name="_iCtsubid"></param>
        /// <returns></returns>
        public static RespBizContractSub GetBizContractSub(int _iProjid, int _iCtid,int _iCtsubid)
        {
            RespBizContractSub respReturn = new RespBizContractSub();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<BizContractSubExtInfo> lstCtSub = bpctx.VIEW_BIZ_CONTRACTSUBINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                                                      && (_iCtid == 0 ? true : n.ctid == _iCtid)
                                                                                                                                                      && (_iCtsubid == 0 ? true : n.ctsubid == _iCtsubid)).AsEnumerable()
                                                                                                                                        .Select(n => new BizContractSubExtInfo
                                                                                                                                        {
                                                                                                                                            ctid = n.ctid,
                                                                                                                                            ctnm = n.ctnm,
                                                                                                                                            ctno = n.ctno,
                                                                                                                                            ctsubid = n.ctsubid,
                                                                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                                            pid = n.pid,
                                                                                                                                            pidnm = n.pidnm,
                                                                                                                                            projid = n.projid,
                                                                                                                                            projnm = n.projnm,
                                                                                                                                            subnm = n.subnm
                                                                                                                                        }).ToList();
                respReturn.result = new ResultBizContractSub { ctsubextlist = lstCtSub };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新业务子合同
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateBizContractSub(string _strMemberid,string _strComcd, BizContractSubCore _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb)
                {
                    #region 增加
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iCtSubid = sysLogic.GetRefNo("CTSUB");
                        if (iCtSubid == 0) throw new BizException("获取流水号失败");
                        if (_collec.ctid == 0) throw new BizException("合同id不能为0");
                        BIZ_CONTRACT_SUB entityIns = new BIZ_CONTRACT_SUB
                        {
                            ctid = _collec.ctid,
                            ctsubid = iCtSubid,
                            pid = "0",
                            subnm = _collec.subnm ?? ""
                        };
                        bpctx.BIZ_CONTRACT_SUB.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        BIZ_CONTRACT_SUB entityUpd = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _collec.ctsubid);
                        if (entityUpd == default(BIZ_CONTRACT_SUB)) throw new BizException("未找到要操作的记录");
                        if (entityUpd.pid == "9") throw new BizException("合同子项已完结，无法修改");
                        entityUpd.subnm = string.IsNullOrEmpty(_collec.subnm) ? entityUpd.subnm : _collec.subnm;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        BIZ_CONTRACT_SUB entityDel = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _collec.ctsubid);
                        if (entityDel == default(BIZ_CONTRACT_SUB)) throw new BizException("未找到要操作的记录");
                        if (entityDel.pid == "9") throw new BizException("合同已完结，无法修改");
                        if (bpctx.BIZ_PROJDETAIL.Count(n => n.ctsubid == entityDel.ctsubid) > 0) throw new BizException("有子记录，不能删除");
                        if (bpctx.BIZ_CONTRACTTB.Count(n => n.ctid == entityDel.ctid && n.pid == "9") > 0) throw new BizException("归属合同已完结，不能删除");
                        //****需加判操作权限
                        bpctx.BIZ_CONTRACT_SUB.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新业务子合同进程
        /// </summary>
        /// <param name="_strMemberid"></param>
        /// <param name="_strComcd"></param>
        /// <param name="_collec"></param>
        /// <returns></returns>
        public static RespProjNullResultInfo UpdateBizContractSubProc(string _strMemberid, string _strComcd, BizContractSubCore _collec)
        {
            RespProjNullResultInfo respReturn = new RespProjNullResultInfo();
            BizProjDataContext bpctx = new BizProjDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_collec.optlb != GlobalSettings.ENTITYOPERATION_UPDATE) throw new Exception("操作标识符异常");
                //****需加判操作权限
                BIZ_CONTRACT_SUB entityUpd = bpctx.BIZ_CONTRACT_SUB.SingleOrDefault(n => n.ctsubid == _collec.ctsubid);
                if (entityUpd == default(BIZ_CONTRACT_SUB)) throw new BizException("未找到要操作的记录");
                entityUpd.pid = string.IsNullOrEmpty(_collec.pid) ? entityUpd.pid : _collec.pid;
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }





    }
}